package com.tudouji.project.bill.service.impl;

import com.tudouji.common.utils.DateUtils;
import com.tudouji.common.utils.IdUtils;
import com.tudouji.project.bill.domain.Sellitem;
import com.tudouji.project.bill.domain.Sellitemchgrec;
import com.tudouji.project.bill.domain.Whoutformitem;
import com.tudouji.project.bill.mapper.SellitemMapper;
import com.tudouji.project.bill.mapper.SellitemchgrecMapper;
import com.tudouji.project.bill.service.ISellitemService;
import com.tudouji.project.system.domain.SysUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 订单明细Service业务层处理
 *
 * @author tudouji
 * @date 2021-01-14
 */
@Service
public class SellitemServiceImpl implements ISellitemService
{
    @Autowired
    private SellitemMapper sellitemMapper;
    @Autowired
    private SellitemchgrecMapper sellitemchgrecMapper;
    @Autowired
    private WhoutformServiceImpl whoutformService;

    /**
     * 查询订单明细
     *
     * @param guid 订单明细ID
     * @return 订单明细
     */
    @Override
    public Sellitem selectSellitemById(String guid)
    {
        return sellitemMapper.selectSellitemById(guid);
    }

    /**
     * 查询订单明细列表
     *
     * @param sellitem 订单明细
     * @return 订单明细
     */
    @Override
    public List<Sellitem> selectSellitemList(Sellitem sellitem)
    {
        return sellitemMapper.selectSellitemList(sellitem);
    }

    /**
     * 新增订单明细
     *
     * @param sellitem 订单明细
     * @return 结果
     */
    @Override
    public int insertSellitem(Sellitem sellitem)
    {
        return sellitemMapper.insertSellitem(sellitem);
    }

    /**
     * 修改订单明细
     *
     * @param sellitem 订单明细
     * @return 结果
     */
    @Override
    public int updateSellitem(Sellitem sellitem)
    {
        return sellitemMapper.updateSellitem(sellitem);
    }

    /**
     * 批量删除订单明细
     *
     * @param guids 需要删除的订单明细ID
     * @return 结果
     */
    @Override
    public int deleteSellitemByIds(String[] guids)
    {
        return sellitemMapper.deleteSellitemByIds(guids);
    }

    /**
     * 删除订单明细信息
     *
     * @param guid 订单明细ID
     * @return 结果
     */
    @Override
    public int deleteSellitemById(String guid)
    {
        return sellitemMapper.deleteSellitemById(guid);
    }

    @Override
    public List<Sellitem> getSellItems(Sellitem si)
    {
        return sellitemMapper.getSellItems(si);
    }

    /**
     * 根据销售单明细id  ,产品id,规格id 更新重量
     * @throws Exception
     * * @Description 修改此方法，添加产品明细互换功能
     *  @Date 15:34 2020/11/6
     */
    public void updatePI(Whoutformitem whoutformitem, SysUser user)
    {
        // 多次出库，销售订单明细
        Sellitem item = sellitemMapper.selectSellitemById(whoutformitem.getItemguid());
        /**产品明细互换功能***/
        if (StringUtils.isNotBlank(whoutformitem.getSpguid()) && StringUtils.isNotBlank(item.getSpguid())){
            if (!item.getSpguid().equalsIgnoreCase(whoutformitem.getSpguid())){
                Sellitemchgrec grec = new Sellitemchgrec();
                grec.setSellitemguid(item.getGuid());
                grec.setB4specguid(item.getSpguid());
                grec.setAfterspecguid(whoutformitem.getSpguid());
                grec.setCreateTime(new Date());
                grec.setOperator(user.getGuid());
                grec.setGuid(IdUtils.fastSimpleUUID());
                sellitemchgrecMapper.insertSellitemchgrec(grec);

                item.setSpguid(whoutformitem.getSpguid());
                item.setProductguid(whoutformitem.getProductguid());
                item.setSpname(whoutformitem.getSpename());
                item.setProductname(whoutformitem.getProduct());
            }
        }
        /**产品明细互换功能***/
        BigDecimal weight = whoutformService.calculateWeightSF(item.getSpguid(),item.getSfunitid(),
                whoutformitem.getWeight(), false);
        item.setLoadweight(weight);
        item.setUpdateBy(user.getUserId());
        item.setUpdateTime(DateUtils.getNowDate());
        sellitemMapper.updateSellitem(item);
    }
}
